<?php
/**
 * 애드페이 (AD-Pay for Gnuboard4)
 *
 * Copyright (c) 2010 Choi Jae-Young <www.miwit.com>
 *
 * 저작권 안내
 * - 저작권자는 이 프로그램을 사용하므로서 발생하는 모든 문제에 대하여 책임을 지지 않습니다. 
 * - 이 프로그램을 어떠한 형태로든 재배포 및 공개하는 것을 허락하지 않습니다.
 * - 이 저작권 표시사항을 저작권자를 제외한 그 누구도 수정할 수 없습니다.
 */

include_once("_common.php");
include_once("_config.php");
include_once("_lib.php");

$sql = " select * from $pb[order_table] where or_id = '$or_id' ";
if (!$is_admin)
    $sql .= " and mb_id = '$member[mb_id]' ";

$order = sql_fetch($sql);
if (!$order)
    alert("데이터가 없습니다.");

$g4[title] = "{$pb[cf_name]} - $order[or_site] 통계";
include_once("$g4[path]/head.sub.php");

if (!$month)
    $month = date("Y-m", $g4[server_time]);

if (!preg_match("/^[0-9]{4}-[0-9]{2}$/", $month))
    alert("$month month 데이터가 올바르지 않습니다.");

$end_date = date("t", strtotime("$month-01 00:00:00"));

$log_table = $pb[log_table] . '_' . date("ym", strtotime("$month-01 00:00:00"));
$output_table = $pb[output_table] . '_' . date("ym", strtotime("$month-01 00:00:00"));

$sql = "show tables like '$output_table'";
$qry = sql_query($sql);
if (!mysql_num_rows($qry))
    alert("데이터가 없습니다.");

$qry = sql_query("show tables like '$log_table'");
if (!mysql_num_rows($qry))
    sql_query(str_replace($pb[log_table], $log_table, $pb_log_sql));

$sql = "select min(ou_date) as sdate, max(ou_date) as edate from $output_table where or_id = '$or_id'";
$row = sql_fetch($sql);
$log_sdate = $row[sdate];
$log_edate = $row[edate];
 
$total_visit = 0;
$total_output = 0;
$total_rate = 0;

for ($i=1; $i<=$end_date; $i++)
{
    $date = "$month-" . sprintf("%02d", $i);

    $sql = "select count(*) as cnt from $log_table where pl_date = '$date' and or_id = '$or_id'";
    $row = sql_fetch($sql);
    $visit_count = $row[cnt];

    $sql = "select count(*) as cnt from $output_table where ou_date = '$date' and or_id = '$or_id'";
    $row = sql_fetch($sql);
    $output_count = $row[cnt];

    if ($visit_count)
        $rate = @round($visit_count/$output_count,2)*100;
    else
        $rate = '';

    $list[$i] = array(
        'date' => $date,
        'visit_count' => $visit_count,
        'output_count' => $output_count,
        'rate' => $rate,
    );

    $total_visit += $visit_count;
    $total_output += $output_count;
}

$total_rate = @round($total_visit/$total_output,2)*100;

?>

<link rel="stylesheet" href="style.css" type="text/css">
<style type="text/css">
body { margin:10px; }
.t { border-top:2px solid #818181; border-bottom:2px solid #818181; }
.t .tt { background-color:#f2f2f2; font:normal 11px 'dotum'; text-align:center; height:30px; color:#3f4ea1; border-bottom:1px solid #d5d5d5; }
.t .tl { border-bottom:1px solid #e8e8e8; height:25px; text-align:center; font:normal 11px 'dotum'; }
.t .tl a { font:normal 11px 'dotum'; }
.t .tn { background-color:#fff; padding:100px 0 100px 0; text-align:center; color:#bbb; border-bottom:1px solid #d5d5d5; }
.b { background-color:#efefef; cursor:pointer; font:normal 11px 'dotum'; }
</style>

<h3><?=$order[or_site]?> 통계
<span style="font:normal 11px 'gulim'; color:#777; text-align:center; height:20px; margin-left:10px;">
(광고게시일 <?=$order[or_sdate]?> ~ <?=$order[or_edate]?> / 데이터 측정일 <?=$log_sdate?> ~ <?=$log_edate?>)
</span></h3>

<script type="text/javascript">
function go_prev() {
    month = "<?=date("Y-m", strtotime("-1 month", strtotime("{$list[1][date]} 00:00:00")))?>";
    location.href = "<?=$_SERVER[PHP_SELF]?>?or_id=<?=$or_id?>&month=" + month;
}

function go_next() {
    month = "<?=date("Y-m", strtotime("+1 month", strtotime("{$list[1][date]} 00:00:00")))?>";
    location.href = "<?=$_SERVER[PHP_SELF]?>?or_id=<?=$or_id?>&month=" + month;
}
</script>

<div style="text-align:center; margin:10px auto 10px auto;">
    <input type="button" value="<<" onclick="go_prev()" class="b">
    &nbsp;<?=$month?>&nbsp;
    <input type="button" value=">>" onclick="go_next()" class="b">
</div>

<table border="0" cellpadding="0" cellspacing="0" width="100%" class="t">
<tr>
    <td class="tt" width="25%"> 일자 </td>
    <td class="tt" width="25%"> 클릭수 </td>
    <td class="tt" width="25%"> 노출수 </td>
    <td class="tt" width="25%"> 클릭율 </td>
</tr>
<?  for ($i=1; $i<=$end_date; $i++) { ?>
<tr>
    <td class="tl"> <?=$list[$i][date]?> </td>
    <td class="tl"> <?=number_format($list[$i][visit_count])?> </td>
    <td class="tl"> <?=number_format($list[$i][output_count])?> </td>
    <td class="tl"> <?=$list[$i][rate]!==''?$list[$i][rate].'%':'-'?> </td>
</tr>
<? } ?>
<tr>
    <td class="tl"> 합계  </td>
    <td class="tl"> <?=number_format($total_visit)?> </td>
    <td class="tl"> <?=number_format($total_output)?> </td>
    <td class="tl"> <?=$total_rate?>% </td>
</tr>
</table>

<p align="center" style="height:70px;">
    <input type="button" value="닫    기" onclick="self.close()" class="b"/>
</p>

<?
include_once("$g4[path]/tail.sub.php");

